package org.bultreebank.testing;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import org.bultreebank.index.utils.Constants;
import org.bultreebank.index.utils.SystemInit;
import org.bultreebank.index.utils.Utils;
import org.bultreebank.search_engine.SearchEngine;
import org.bultreebank.search_engine.SearchEngineImpl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import clark.xpath.MainXPath;

public class ConceptAnnotationIndexTest implements Constants {
	public static List<String> setAutogeneratedIDs(Document sourceDoc, 
			String absoluteXPATHSelection, String attrName,
			String sourceValue)
			throws Exception
	{
		List<String> IDs = new ArrayList<String>();
		Element docElement = sourceDoc.getDocumentElement();
		List resNodeList = MainXPath.processXPath(absoluteXPATHSelection,
      		   docElement);
		int i = 1;
		for (Object elem:resNodeList)
		{
			if (resNodeList != null && !resNodeList.isEmpty() )
			{
				String generatedID = sourceValue + i; 
				((Element) elem).setAttribute(attrName, generatedID);
				IDs.add(generatedID);
				
				i++;
			}
		}
		return IDs; 
	}
	
	public static int docLength(Document doc)
	{
		String xpath = "//Concept | //instance";
		List<Node> nodes = MainXPath.processXPath(xpath, doc.getDocumentElement());
		return nodes.size();
	}
	public static void main(String[] args) {

		String docsToIndex = "Test/DocumentsResult_brackets";
		String repositoryPath = "Test/IndexRepository";
		try {
		    SearchEngine engine = new SearchEngineImpl(repositoryPath);
			String pathMetaData = "Test/Metadata";
			String configFile = Utils.getStringRepresentationOfFileContent("Test/IndexingSchemaParsImage.xml");
			engine.registerIndex(configFile);
			File documents = new File(docsToIndex);
			File [] files = documents.listFiles();
			
			StringBuffer buf = new StringBuffer();
			double tempT = 0.0;
			int tokens = 0, concepts = 0;
					
			Date start = new Date();
			for (int i = 0; i < files.length ; i++)
			{
				String fileName = files[i].getName();
				
				String tempFileName = fileName.substring(fileName.indexOf("_") - 1).substring(fileName.indexOf("_"));//, fileName.indexOf("_"));
				String doc = Utils.getStringRepresentationOfFileContent(docsToIndex + 
						java.io.File.separator + fileName);
				Document d = Utils.getDocumentFromString(doc);
				setAutogeneratedIDs(d, "//par/s", "id", "s");
				setAutogeneratedIDs(d, "//par", "id", "p");
				
				Utils.saveDocument(docsToIndex + 
						java.io.File.separator + fileName, null, d);
				doc = Utils.getStringRepresentationOfFileContent(docsToIndex + 
						"/" + fileName);
				File metaF = new File(pathMetaData);
				File[] filesM = metaF.listFiles();
				for (int j = 0; j < filesM.length; j++)
				{
					if (!filesM[j].isDirectory())
					{
						String metaDataName = filesM[j].getName();
						String temp = metaDataName;
						metaDataName = metaDataName.substring(0, metaDataName.indexOf("_"));
						if (tempFileName.startsWith(metaDataName))
						{
								
							String metaDoc = Utils.getStringRepresentationOfFileContent(pathMetaData + "/" + temp);
							Document xmlDoc = Utils.getDocumentFromString(doc);
							tokens += Utils.docLength(xmlDoc);
							concepts += ConceptAnnotationIndexTest.docLength(xmlDoc);
							engine.indexDocument(doc, metaDoc, "ContextsIndex", null, true);
							
							if ((i != 0 && i%20 == 0) || i == 135)
							{
								Date end = new Date();								
								tempT += (double) (end.getTime() - start.getTime())/1000;
								start = new Date();
								buf.append(tempT + " --> " + tokens + " --> " + concepts + ", ");
								tokens = 0;
								concepts = 0;
							}
							System.out.println("Document: " + fileName + " is indexed successfully!");
						}
					}
				}
			
			}
			System.out.println("Time: " + buf.toString());
		}
		 catch (Exception e) {
			e.printStackTrace();
		}
	}
}
